package org.jeecg.modules.pjinfo.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.pjinfo.entity.PjInfo;
import org.jeecg.modules.pjinfo.service.IPjInfoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: PJ_INFO
 * @Author: jeecg-boot
 * @Date:   2021-08-24
 * @Version: V1.0
 */
@Api(tags="PJ_INFO")
@RestController
@RequestMapping("/pjinfo/pjInfo")
@Slf4j
public class PjInfoController extends JeecgController<PjInfo, IPjInfoService> {
	@Autowired
	private IPjInfoService pjInfoService;

	@Autowired
	private  ISysBaseAPI isysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param pjInfo
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-分页列表查询")
	@ApiOperation(value="PJ_INFO-分页列表查询", notes="PJ_INFO-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PjInfo pjInfo,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PjInfo> queryWrapper = QueryGenerator.initQueryWrapper(pjInfo, req.getParameterMap());
		Page<PjInfo> pageList = new Page<PjInfo>(pageNo, pageSize);
		pageList = pjInfoService.querypjinfolists(pageList, pjInfo);
		return Result.OK(pageList);
	}





	 /**
	  * 分页审核列表查询
	  *
	  * @param pjInfo
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "PJ_INFO-分页审核列表查询")
	 @ApiOperation(value="PJ_INFO-分页审核列表查询", notes="PJ_INFO-分页审核列表查询")
	 @GetMapping(value = "/listsh")
	 public Result<?> queryPageListsh(PjInfo pjInfo,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<PjInfo> queryWrapper = QueryGenerator.initQueryWrapper(pjInfo, req.getParameterMap());
		 Page<PjInfo> pageList = new Page<PjInfo>(pageNo, pageSize);
		 pageList = pjInfoService.querypjinfoshlist(pageList, pjInfo);
		 return Result.OK(pageList);
	 }




	/**
	 *   创建批次评价
	 *
	 * @param pjInfo
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-创建批次评价")
	@ApiOperation(value="PJ_INFO-创建批次评价", notes="PJ_INFO-创建批次评价")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody PjInfo pjInfos) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		//获取部门id
		//String depid  = pjInfoService.getdepid(pjInfos.getPjBm());
		//获取用户id组
        List<Map<String,Object>> list =  pjInfoService.getuser(pjInfos.getPjBm());

        //获取部门负责人 ---审核人
        List<Map<String,Object>> usersh = pjInfoService.getuseridbydepart(pjInfos.getPjBm());

        if(usersh.isEmpty()){
        	return Result.error("此部门未设置负责人，请联系管理员设置负责人！");
		}

		for (int i = 0; i <list.size() ; i++) {
			PjInfo pjinfo = new  PjInfo();
			//获取用户
			List<Map<String,Object>> userlist = pjInfoService.getinfobyuserid(list.get(i).get("ID").toString());

			if(userlist.size()!=0){
				pjinfo.setPjBm(pjInfos.getPjBm());
				pjinfo.setPjPc(pjInfos.getPjPc());
				System.out.println(pjInfos.getPjPc());
				pjinfo.setFileguid("temp/评分原则.xls");
				pjinfo.setKhTime(pjInfos.getKhTime());
				pjinfo.setPjDfuser(userlist.get(0).get("REALNAME").toString());
				pjinfo.setPjUser(userlist.get(0).get("REALNAME").toString());
				pjinfo.setPjUserid(userlist.get(0).get("ID").toString());
				pjinfo.setPjShuserid(usersh.get(0).get("ID").toString());
				pjinfo.setShUser(usersh.get(0).get("REALNAME").toString());
				pjInfoService.save(pjinfo);
			}


		}

		return Result.OK("创建成功！");
	}


	 /**
	  * 查询所有部门
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "PJ_INFO-查询所有部门")
	 @ApiOperation(value="PJ_INFO-查询所有部门", notes="PJ_INFO-查询所有部门")
	 @GetMapping(value = "/getallbm")
	 public Result<?> getAllBm() {

		 List<DictModel> list = 	 isysBaseAPI.queryAllDepartBackDictModel();
		 System.out.println(list);
		 return Result.OK();
	 }

	/**
	 *  编辑
	 *
	 * @param pjInfo
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-编辑")
	@ApiOperation(value="PJ_INFO-编辑", notes="PJ_INFO-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PjInfo pjInfo) {
		pjInfoService.updateById(pjInfo);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-通过id删除")
	@ApiOperation(value="PJ_INFO-通过id删除", notes="PJ_INFO-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		pjInfoService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-批量删除")
	@ApiOperation(value="PJ_INFO-批量删除", notes="PJ_INFO-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.pjInfoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "PJ_INFO-通过id查询")
	@ApiOperation(value="PJ_INFO-通过id查询", notes="PJ_INFO-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PjInfo pjInfo = pjInfoService.getById(id);
		if(pjInfo==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(pjInfo);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param pjInfo
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, PjInfo pjInfo) {
        return super.exportXls(request, pjInfo, PjInfo.class, "价值观评价");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, PjInfo.class);
    }

}
